﻿using ConsoleHoster.Common.Notifications;
using ConsoleHoster.Common.Utilities.Threading;
using ConsoleHoster.ViewModel;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using System.Collections.Generic;

namespace ConsoleHoster.UnitTests.ViewModelTests
{
	[TestClass]
	public class ProjectListViewModelTests
	{
		private Mock<IUserInteractionManager> mockUserInteractionManager;
		private Mock<IDispatcher> mockDispatcher;
		private ProjectsListViewModel sut;

		[TestInitialize]
		public void Initialize()
		{
			this.mockUserInteractionManager = new Mock<IUserInteractionManager>();
			this.mockDispatcher = new Mock<IDispatcher>();
		}

		[TestCleanup]
		public void Cleanup()
		{
			this.sut = null;
		}

		[TestMethod]
		public void RemoveProject_RequestsConfirmationThroughUserInteractionManager()
		{
			IList<ProjectDetailsViewModel> tmpProjectsList = new List<ProjectDetailsViewModel>();
			TestRepository tmpRepository = new TestRepository();
			ProjectDetailsViewModel tmpVM = new ProjectDetailsViewModel(new ViewModel.Entities.ConsoleProjectViewModel(tmpRepository.ConsoleProject1), true, s => true, this.Dispatcher, null);
			tmpProjectsList.Add(tmpVM);

			this.mockUserInteractionManager
				.Setup(um => um.Confirm(It.IsAny<string>(), It.IsAny<string>(), null))
				.Returns(true);

			this.sut = new ProjectsListViewModel(tmpProjectsList, this.UserInteractionManager, this.Dispatcher, null);

			Assert.AreEqual(1, this.sut.Projects.Count);
			this.sut.RemoveProject(tmpVM);
			Assert.AreEqual(0, this.sut.Projects.Count, "Project should have been removed");

			this.mockUserInteractionManager.Verify(um => um.Confirm(It.IsAny<string>(), It.IsAny<string>(), null), Times.Once());

			Assert.IsNull(this.sut.SelectedProject);
		}

		private IUserInteractionManager UserInteractionManager
		{
			get
			{
				return this.mockUserInteractionManager.Object;
			}
		}

		private IDispatcher Dispatcher
		{
			get
			{
				return this.mockDispatcher.Object;
			}
		}
	}
}
